package com.itCar.base.test.practical;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

/**
 * @ClassName: XSSFWriter
 * @Description: TODO
 * @author: liuzg
 * @Date: 2022/9/6 15:51
 * @Week: 星期二
 * @Version: v1.0
 */
public class XSSFWriter {
    private static SXSSFWorkbook wb;

    public static void main(String[] args) throws IOException {
        wb = new SXSSFWorkbook(10000);
        Sheet sh = wb.createSheet();
        for(int rownum = 0; rownum < 100000; rownum++){
            Row row = sh.createRow(rownum);
            for(int cellnum = 0; cellnum < 10; cellnum++){
                Cell cell = row.createCell(cellnum);
                String address = new CellReference(cell).formatAsString();
                cell.setCellValue(address);
            }

        }

        // Rows with rownum < 900 are flushed and not accessible
//        for(int rownum = 0; rownum < 90000; rownum++){
//            Assert.assertNull(sh.getRow(rownum));
//        }

        // ther last 100 rows are still in memory
//        for(int rownum = 90000; rownum < 100000; rownum++){
//            Assert.assertNotNull(sh.getRow(rownum));
//        }
        URL url = XSSFWriter.class.getClassLoader().getResource("");

        FileOutputStream out = new FileOutputStream(url.getPath() + File.separator + "wirter.xlsx");
        wb.write(out);
        out.close();

        // dispose of temporary files backing this workbook on disk
        wb.dispose();
    }

    /*

    根据导出
    @Override
    public Workbook getFacilityStatusInfoExport_New(List<LinkedHashMap> list, String code) {
        Workbook workbook = null;
        InputStream inputStream = null;
//        String tmpPath = null;
        try {
            inputStream = this.getClass().getResourceAsStream("/templates/risk/设施状况明细_新.xlsx");
//            tmpPath = this.getClass().getResource("/templates/export/tmp").getPath();

            LinkedHashMap lmap = list.get(0);
            Iterator<?> it = lmap.keySet().iterator();
            ArrayList<String> keyList = new ArrayList<>();
            while (it.hasNext()) {
                String key = it.next().toString();
                keyList.add(key);
            }
            XSSFWorkbook xssworkbook = new XSSFWorkbook(inputStream);
            workbook = new SXSSFWorkbook(xssworkbook, 200, true);
//            TempFile.setTempFileCreationStrategy(new TempFile.DefaultTempFileCreationStrategy(new File(tmpPath)));
//            TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(new File(tmpPath)));
            workbook.setSheetName(0, "设施状况明细");
            Sheet sheet = ((SXSSFWorkbook) workbook).getXSSFWorkbook().getSheetAt(0);
            XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle();
            style.setAlignment(CENTER);//水平居中
            Row rowTitle = sheet.getRow(0);
            Cell cellTitle = rowTitle.getCell(0);
            cellTitle.setCellValue(this.getDocumentTitle(code, "民防工程防护设施状况明细表"));

//            cellTitle.setCellStyle(sheet.getRow(0).getCell(0).getCellStyle());
            for (int rownum = 0; rownum < list.size(); rownum++) {
                Row row = sheet.createRow(rownum + 3);
                for (int cellnum = 0; cellnum < keyList.size(); cellnum++) {
                    Cell cell = row.createCell(cellnum);
                    String value =  list.get(rownum).get(keyList.get(cellnum))==null?" ":list.get(rownum).get(keyList.get(cellnum)).toString();
                    cell.setCellValue(value);
                    cell.setCellStyle(style);
                }
            }
        } catch (Exception e) {
//            logger.error("生成工作薄失败："+ e.getMessage());
            return null;
        }

        return workbook;
     */
}
